//
//  JDVisitorLoginView.swift
//  JDWeiBo
//
//  Created by apple on 15/6/24.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

protocol JDVisitorLoginViewDelegate : NSObjectProtocol {
    
    // 注册
    func RegisterButtonClick()
    // 登录
    func LoginButtonClick()
    
}

class JDVisitorLoginView: UIView {
    
    weak var delegate : JDVisitorLoginViewDelegate?
    
    // 注册方法
    func register() {
        delegate?.RegisterButtonClick()
    }
    
    func Login() {
        delegate?.LoginButtonClick()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }
    
    required init(coder aDecoder: NSCoder) {
         // 用 sb 或者 xib 时候会调用这个方法
        fatalError("init(coder:) has not been implemented")
    }
    /// 设置label内容
    func setUpViewInfo(iconName: String, mess:String, isHome: Bool) {
        
        messLabel.text = mess
        
        if isHome {
            homeIconView.hidden = false
            iconView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        } else {
            homeIconView.hidden = true
            iconView.image = UIImage(named: iconName)
            sendSubviewToBack(maskIconImage)
        }
    }
    
    /// 动画转动
    func startAnmiation() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        
        anim.duration = 10
        
        anim.repeatCount = MAXFLOAT
        
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    private func setUpUI() {
        
        addSubview(iconView)
        addSubview(maskIconImage)
        addSubview(homeIconView)
        addSubview(messLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
    /// 自动布局
        // 转动图标
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        /// 1.对谁布局 / 2.对那个边属性对齐方式 / 3.与谁相关 / 4.参考控件是谁（self） / 5.与参考控件的那个属性对齐方式 / 6.一般都是1.0（倍数） / 7.对参考控件的间距多少
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -70))
        
        // 中间房子
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -70))
        
        // 文本标签
        messLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: messLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: messLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        
        // 注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(80)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":registerButton]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":registerButton]))
        
        // 登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(80)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":loginButton]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":loginButton]))
        
        // 遮盖背景
        maskIconImage.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subView]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":maskIconImage]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subView]-(160)-|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":maskIconImage]))
        
        // 设置背景颜色 遮盖下边
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
    
    ///  懒加载
    // 转动图标
    lazy var iconView : UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return UIImageView(image: image)
    }()
    // 遮盖背景
    lazy var maskIconImage : UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: image)
    }()
    // 中间房子
    lazy var homeIconView : UIImageView = {
    
        let image = UIImage(named: "visitordiscover_feed_image_house")
        return UIImageView(image: image)
    }()
    // 文本标签
    lazy var messLabel : UILabel = {
    
        let label = UILabel()
        label.text = "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"
        label.textColor = UIColor.darkGrayColor()
        label.textAlignment = NSTextAlignment.Center
        label.preferredMaxLayoutWidth = 220
        label.numberOfLines = 0
        label.sizeToFit()
        return label
    }()
    // 注册按钮
    lazy var registerButton : UIButton = {
    
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    // 登录按钮
    lazy var loginButton : UIButton = {
    
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "Login", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
}